package com.gitee.feizns.explore.data_structure.tree.bs.easy;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author feizns
 * @since 2019/12/25
 */
public class BinaryTreePaths {

    public static void main(String[] args) {
        binaryTreePaths(create()).forEach(System.out::println);
    }

    private static TreeNode create() {
        TreeNode ret = new TreeNode(1);
        ret.left = new TreeNode(2);
        ret.right = new TreeNode(3);
        ret.left.right = new TreeNode(5);
        return ret;
    }

    public static List<String> binaryTreePaths(TreeNode root) {
        if ( root != null ) {
            List<String> leftPaths = binaryTreePaths(root.left);
            List<String> rightPaths = binaryTreePaths(root.right);
            leftPaths.addAll(rightPaths);
            if ( !leftPaths.isEmpty() )
                for (int i = 0; i < leftPaths.size(); i++)
                    leftPaths.set(i, root.val + "->" + leftPaths.get(i));
            else
                return new ArrayList(Arrays.asList(String.valueOf(root.val)));
            return leftPaths;
        }
        return new ArrayList<>();
    }

}
